import cv2 as cv

img = cv.imread('C:/Users/Administrator/Desktop/opencv/1.jpg')#加载
gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)#灰度图
face_detector = cv.CascadeClassifier('C:/Users/Administrator/Desktop/opencv/sources/data/haarcascades/haarcascade_frontalface_alt.xml')# 创建级联分类器
faces = face_detector.detectMultiScale(gray,1.1,5)# 对图像进行检测
for x,y,w,h in faces:
    cv.rectangle(img,(x,y),(x+w,y+h),(0,0,255),2)
cv.imshow('result',img)
cv.waitKey(0)
cv.destroyAllWindows() #必备

